package org.jeecg.modules.business.strategy;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.basic.material.entity.WmsBasicMaterialStockInfo;
import org.jeecg.modules.basic.material.service.IWmsBasicMaterialStockInfoService;
import org.jeecg.modules.basic.plant.entity.WmsSapWarehouseMap;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseAreaLocation;
import org.jeecg.modules.basic.warehouse.mapper.WmsBasicWarehouseAreaLocationMapper;
import org.jeecg.modules.basic.warehouse.mapper.WmsBasicWarehouseAreaMapper;
import org.jeecg.modules.basic.warehouse.mapper.WmsBasicWarehouseMapper;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.mapper.WmsOutReceiptTaskMapper;
import org.jeecg.modules.business.outbound.mapper.WmsOutReceiptTaskMaterialMapper;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.mapper.WmsRepertoryMapper;
import org.jeecg.modules.business.strategy.entity.InReceiptStrategyParam;
import org.jeecg.modules.business.strategy.entity.InReceiptStrategyWarehouse;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.mapper.WmsInReceiptTaskMapper;
import org.jeecg.modules.business.warehouse.mapper.WmsInReceiptTaskMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : lumingyi
 * @date : 2023/11/24 10:58
 * @description : 入库策略
 * @Version : 1.0
 */
@Component
@Slf4j
public class InReceiptStrategy {

    private static final String SIZING_PRODUCT_SITE = "AZ-5-01";  //定尺包装线

    @Autowired
    private WmsBasicWarehouseMapper warehouseMapper;
    @Autowired
    private WmsBasicWarehouseAreaMapper warehouseAreaMapper;
    @Autowired
    private WmsBasicWarehouseAreaLocationMapper warehouseAreaLocationMapper;
    @Autowired
    private ISysBaseAPI iSysBaseAPI;
    @Autowired
    private WmsRepertoryMapper repertoryMapper;
    @Autowired
    private WmsOutReceiptTaskMapper outReceiptTaskMapper;
    @Autowired
    private WmsOutReceiptTaskMaterialMapper outReceiptTaskMaterialMapper;
    @Autowired
    private WmsInReceiptTaskMapper inReceiptTaskMapper;
    @Autowired
    private WmsInReceiptTaskMaterialMapper inReceiptTaskMaterialMapper;
    @Autowired
    private IWmsSapWarehouseMapService sapWarehouseMapService;
    @Autowired
    private IWmsBasicMaterialStockInfoService materialStockInfoService;

    /**
     * 获取仓库
     * 来源系统=MES 并且 单据类型= 成品入库-铁心、成品入库-定尺时，获取仓库和库区
     * @param params
     * @return
     */
    public List<InReceiptStrategyWarehouse> warehouseStrategy(List<InReceiptStrategyParam> params) {
        List<InReceiptStrategyWarehouse> strategyList = new ArrayList<>();
        for (InReceiptStrategyParam param : params) {
            InReceiptStrategyWarehouse strategy = new InReceiptStrategyWarehouse();
            // 找仓库
            WmsBasicWarehouse warehouse = this.getWarehouse(param);
            if (warehouse == null || StringUtils.isBlank(warehouse.getId())) {
                strategyList.add(strategy);
                continue;
            }
            String warehouseId = warehouse.getId();
            strategy.setWarehouseId(warehouseId);
            // 来源系统=MES 并且 单据类型= 成品入库-铁心、成品入库-定尺时，获取仓库和库区
    //        if ("MES".equals(param.getOriginalSystem()) && ("R25".equals(param.getDocumentType()) || "R26".equals(param.getDocumentType())) && StringUtils.isBlank(param.getWarehouseId())) {
    //            // 来源系统=MES并且 单据类型= 成品入库-铁心、成品入库-定尺
    //            MesSite one = mesSiteService.getOne(new LambdaQueryWrapper<MesSite>()
    //                    .eq(MesSite::getAskMaterialSite, param.getProductSite())
    //                    .last("limit 1"));
    //            if (one != null) {
    //                strategy.setWarehouseId(one.getMesInWarehouseId());
    //                strategy.setWarehouseAreaId(one.getWarehouseAreaId());
    //            }
    //        }
            strategyList.add(strategy);
        }
        return strategyList;
    }

    /**
     * 纵剪入库(条料) 、一二期-四期调拨入库(条料)
     * @return
     */
//    public InReceiptStrategyWarehouse stripMaterialInReceipt(InReceiptStrategyParam param) {
//        InReceiptStrategyWarehouse strategy = new InReceiptStrategyWarehouse();
//        // 找仓库
//        WmsBasicWarehouse warehouse = this.getWarehouse(param);
//        if (warehouse == null || StringUtils.isBlank(warehouse.getId())) {
//            return null;
//        }
//        String warehouseId = warehouse.getId();
//        strategy.setWarehouseId(warehouseId);
////        return strategy;
//
//        // 找库区
//        List<DictModel> sites = iSysBaseAPI.getDictItems("cross_cut_material_site");    // 生产站点
//        List<DictModel> siteList = sites.stream().filter(a -> StringUtils.isNotBlank(param.getProductSite()) && param.getProductSite().equals(a.getText())).collect(Collectors.toList());
//        List<WmsBasicWarehouseArea> areas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
//                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
//        // 定尺包装线
//        if (SIZING_PRODUCT_SITE.equals(param.getProductSite())) {
//            if (CollectionUtils.isNotEmpty(siteList)) {
//                // 根据计划生产站点匹配库区
//                List<WmsBasicWarehouseArea> areaList = areas.stream().filter(a -> StringUtils.isNotBlank(a.getProductSite()) && Arrays.asList(a.getProductSite().split(",")).contains(siteList.get(0).getValue())).collect(Collectors.toList());
//                if (CollectionUtils.isNotEmpty(areaList)) {
//                    strategy.setWarehouseAreaId(areaList.get(0).getId());
//                    return strategy;
//                }
//            }
//        }
//        // 站点不是定尺包装线
//        if (StringUtils.isNotBlank(param.getProductSite()) && !SIZING_PRODUCT_SITE.equals(param.getProductSite())) {
//            if (CollectionUtils.isNotEmpty(siteList)) {
//                // 根据计划生产站点匹配库区
//                List<WmsBasicWarehouseArea> areaList = areas.stream().filter(a -> StringUtils.isNotBlank(a.getProductSite()) && Arrays.asList(a.getProductSite().split(",")).contains(siteList.get(0).getValue())).collect(Collectors.toList());
//                if (CollectionUtils.isNotEmpty(areaList)) {
//                    strategy.setWarehouseAreaId(areaList.get(0).getId());
//                    if (StringUtils.isNotBlank(param.getPlanProductOrderNumber())) {
//                        // 到【即时库存】中找计划生产工单号=本次入库的计划生产工单号
//                        List<WmsRepertory> repertoryList = repertoryMapper.selectList(new LambdaQueryWrapper<WmsRepertory>()
//                                .eq(WmsRepertory::getWmsBasicWarehouseAreaId, strategy.getWarehouseAreaId())
//                                .eq(WmsRepertory::getPlanProductOrderNumber, param.getPlanProductOrderNumber()));
//                        if (CollectionUtils.isNotEmpty(repertoryList)) {
//                            List<String> locationIds = repertoryList.stream().map(WmsRepertory::getWmsBasicWarehouseAreaLocationId).filter(StringUtils::isNotBlank).collect(Collectors.toList());
//                            this.getLocation(strategy, warehouse, param, locationIds);
//                            if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
//                                return strategy;
//                            }
//                        }
//                        this.getLocation(strategy, warehouse, param, null);
//                        if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
//                            return strategy;
//                        }
//                    }
//                }
//            }
//        }
//        // 同库区未找到满足条件的库位 or 入库任务.计划生产站点=空
//        if (StringUtils.isBlank(param.getProductSite()) || StringUtils.isBlank(strategy.getWarehouseAreaLocationId())) {
//            strategy.setWarehouseAreaId(null);
//            BigDecimal width = param.getWidth();
//            areas = areas.stream().filter(a -> {
//                if (StringUtils.isBlank(a.getWidthMax()) && StringUtils.isBlank(a.getWidthMin())) {
//                    return false;
//                }
//                if (StringUtils.isNotBlank(a.getWidthMin())) {
//                    if (width.compareTo(new BigDecimal(a.getWidthMin())) != 1) {
//                        return false;
//                    }
//                }
//                if (StringUtils.isNotBlank(a.getWidthMax())) {
//                    if (width.compareTo(new BigDecimal(a.getWidthMax())) == 1) {
//                        return false;
//                    }
//                }
//                return true;
//            }).collect(Collectors.toList());
////            if (CollectionUtils.isNotEmpty(areas)) {
////                strategy.setWarehouseAreaId(areas.get(0).getId());
////                this.getLocation(strategy, warehouse, param, null);
////                if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
////                    return strategy;
////                }
////            }
//
////            if (width.compareTo(new BigDecimal("350")) == 1) {
////                // 片宽＞350mm
////                WmsBasicWarehouseArea one = warehouseAreaMapper.selectOne(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
////                        .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId)
////                        .like(WmsBasicWarehouseArea::getName, "C")
////                        .last("limit 1"));
////                if (one != null) {
////                    strategy.setWarehouseAreaId(one.getId());
////                    this.getLocation(strategy, warehouse, param);
////                    if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
////                        return strategy;
////                    }
////                }
////            } else if (width.compareTo(new BigDecimal("250")) == 1 && width.compareTo(new BigDecimal("350")) != 1) {
////                // 350mm≥片宽＞250mm
////                WmsBasicWarehouseArea one = warehouseAreaMapper.selectOne(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
////                        .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId)
////                        .like(WmsBasicWarehouseArea::getName, "B")
////                        .last("limit 1"));
////                if (one != null) {
////                    strategy.setWarehouseAreaId(one.getId());
////                    this.getLocation(strategy, warehouse, param);
////                    if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
////                        return strategy;
////                    }
////                }
////                WmsBasicWarehouseArea area = warehouseAreaMapper.selectOne(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
////                        .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId)
////                        .like(WmsBasicWarehouseArea::getName, "C")
////                        .last("limit 1"));
////                if (area != null) {
////                    strategy.setWarehouseAreaId(area.getId());
////                    this.getLocation(strategy, warehouse, param);
////                    if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
////                        return strategy;
////                    }
////                }
////            }
//
////            strategy.setWarehouseAreaId(null);
//            if (CollectionUtils.isEmpty(areas)) {
//                // b.找“位置号”比自己大一位的条料的库位
//                if (StringUtils.isBlank(param.getLocation())) {
//                    return strategy;
//                }
//                WmsInReceiptTaskMaterial material = inReceiptTaskMaterialMapper.selectOne(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
//                        .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, param.getTaskId())
//                        .eq(WmsInReceiptTaskMaterial::getLocation, Integer.parseInt(param.getLocation()) + 1)
//                        .last("limit 1"));
//                if (material == null || StringUtils.isBlank(material.getWmsBasicWarehouseAreaId())) {
//                    // c.找“位置号”比自己小一位的条料的库位
//                    material = inReceiptTaskMaterialMapper.selectOne(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
//                            .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, param.getTaskId())
//                            .eq(WmsInReceiptTaskMaterial::getLocation, Integer.parseInt(param.getLocation()) - 1)
//                            .isNotNull(WmsInReceiptTaskMaterial::getWmsBasicWarehouseAreaId)
//                            .last("limit 1"));
//                }
//                if (material != null) {
//                    strategy.setWarehouseAreaId(material.getWmsBasicWarehouseAreaId());
//                    strategy.setWarehouseAreaLocationId(material.getWmsBasicWarehouseAreaLocationId());
//                    return strategy;
//                }
//            }
//            // d.如果找到多个库区，则考虑即时库存中空位量>=【仓库信息】.【库区】设置的值，才是可用库区
////            List<WmsBasicWarehouseArea> areaList = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
////                    .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
//            if (CollectionUtils.isNotEmpty(areas)) {
//                if (areas.size() == 1) {
//                    strategy.setWarehouseAreaId(areas.get(0).getId());
//                    // 找库位
//                    this.getLocation(strategy, warehouse, param, null);
//                    return strategy;
//                }
//                areas = areas.stream().filter(a -> {
//                    String emptyCapacity = a.getEmptyCapacity();
//                    if (StringUtils.isNotBlank(emptyCapacity)) {
//                        List<WmsRepertory> repertories = repertoryMapper.selectList(new LambdaQueryWrapper<WmsRepertory>()
//                                .eq(WmsRepertory::getWmsBasicWarehouseAreaId, a.getId())
//                                .gt(WmsRepertory::getStockQuantity, 0));
//                        Set<String> collect = repertories.stream().map(WmsRepertory::getWmsBasicWarehouseAreaLocationId).collect(Collectors.toSet());
//                        Long count = warehouseAreaLocationMapper.selectCount(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
//                                .eq(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, a.getId())
//                                .eq(WmsBasicWarehouseAreaLocation::getStatus, "1"));
//                        return new BigDecimal(count).subtract(new BigDecimal(collect.size())).compareTo(new BigDecimal(emptyCapacity)) != -1;
//                    }
//                    return true;
//                }).collect(Collectors.toList());
//                this.getOrderByArea(areas, warehouse);
//                strategy.setWarehouseAreaId(areas.get(0).getId());
//                // 找库位
//                this.getLocation(strategy, warehouse, param, null);
//                return strategy;
//            }
//        }
//        return strategy;
//    }

    /**
     * 纵剪入库(条料) 、一二期-四期调拨入库(条料)
     * @return
     */
    public List<InReceiptStrategyWarehouse> stripMaterialInReceipt(List<InReceiptStrategyParam> params) {
        List<InReceiptStrategyWarehouse> strategyList = new ArrayList<>();
        List<InReceiptStrategyParam> paramList = params.stream().filter(a ->
                StringUtils.isNotBlank(a.getWarehouseId())).collect(Collectors.toList());
        String warehouseId = "";
        if (CollectionUtils.isNotEmpty(paramList)) {
            warehouseId = paramList.get(0).getWarehouseId();
        }
        List<DictModel> sites = iSysBaseAPI.getDictItems("cross_cut_material_site");    // 生产站点
        List<WmsBasicWarehouseArea> allAreas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(StringUtils.isNotBlank(warehouseId), WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
        List<String> areaIds = allAreas.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
        List<WmsBasicWarehouseAreaLocation> allLocations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, areaIds));
        List<WmsRepertory> allRepertory = repertoryMapper.selectList(new LambdaQueryWrapper<WmsRepertory>()
                .eq(StringUtils.isNotBlank(warehouseId), WmsRepertory::getWmsBasicWarehouseId, warehouseId));
        List<WmsOutReceiptTask> outTasks = outReceiptTaskMapper.selectList(new LambdaQueryWrapper<WmsOutReceiptTask>()
                .in(WmsOutReceiptTask::getDocumentStatus, Arrays.asList("2", "5")));
        List<WmsOutReceiptTaskMaterial> outs = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(outTasks)) {
            List<String> outIds = outTasks.stream().map(WmsOutReceiptTask::getId).collect(Collectors.toList());
            outs = outReceiptTaskMaterialMapper.selectList(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                    .in(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, outIds)
                    .gt(WmsOutReceiptTaskMaterial::getUnoutboundQuantity, 0));
        }
        List<WmsInReceiptTask> inTasks = inReceiptTaskMapper.selectList(new LambdaQueryWrapper<WmsInReceiptTask>()
                .in(WmsInReceiptTask::getDocumentStatus, Arrays.asList("2", "5")));
        List<WmsInReceiptTaskMaterial> materialList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(inTasks)) {
            List<String> ids = inTasks.stream().map(WmsInReceiptTask::getId).collect(Collectors.toList());
            materialList = inReceiptTaskMaterialMapper.selectList(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                    .in(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, ids)
                    .gt(WmsInReceiptTaskMaterial::getUnwarehousedQuantity, 0));   // 未入库数量大于0
        }
        for (InReceiptStrategyParam param : params) {
            InReceiptStrategyWarehouse strategy = new InReceiptStrategyWarehouse();
            strategy.setId(param.getId());
            // 找仓库
            WmsBasicWarehouse warehouse = this.getWarehouse(param);
            if (warehouse == null || StringUtils.isBlank(warehouse.getId())) {
                strategyList.add(strategy);
                continue;
            }
            warehouseId = warehouse.getId();
            strategy.setWarehouseId(warehouseId);

            // 找库区
            List<DictModel> siteList = sites.stream().filter(a -> StringUtils.isNotBlank(param.getProductSite()) && param.getProductSite().equals(a.getText())).collect(Collectors.toList());
            List<WmsBasicWarehouseArea> areas = allAreas;
            // 定尺包装线
            if (SIZING_PRODUCT_SITE.equals(param.getProductSite())) {
                if (CollectionUtils.isNotEmpty(siteList)) {
                    // 根据计划生产站点匹配库区
                    List<WmsBasicWarehouseArea> areaList = areas.stream().filter(a -> StringUtils.isNotBlank(a.getProductSite()) && Arrays.asList(a.getProductSite().split(",")).contains(siteList.get(0).getValue())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(areaList)) {
                        strategy.setWarehouseAreaId(areaList.get(0).getId());
                        strategyList.add(strategy);
                        continue;
                    }
                }
            }
            // 站点不是定尺包装线
            if (StringUtils.isNotBlank(param.getProductSite()) && !SIZING_PRODUCT_SITE.equals(param.getProductSite())) {
                if (CollectionUtils.isNotEmpty(siteList)) {
                    // 根据计划生产站点匹配库区
                    List<WmsBasicWarehouseArea> areaList = areas.stream().filter(a -> StringUtils.isNotBlank(a.getProductSite())
                            && Arrays.asList(a.getProductSite().split(",")).contains(siteList.get(0).getValue()))
                            .collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(areaList)) {
                        strategy.setWarehouseAreaId(areaList.get(0).getId());
                        if (StringUtils.isNotBlank(param.getPlanProductOrderNumber())) {
                            // 到【即时库存】中找计划生产工单号=本次入库的计划生产工单号
                            List<WmsRepertory> repertoryList = allRepertory.stream()
                                    .filter(a -> ObjectUtil.equal(strategy.getWarehouseAreaId(), a.getWmsBasicWarehouseAreaId())
                                            && ObjectUtil.equal(param.getPlanProductOrderNumber(), a.getPlanProductOrderNumber())).collect(Collectors.toList());
                            if (CollectionUtils.isNotEmpty(repertoryList)) {
                                List<String> locationIds = repertoryList.stream().map(WmsRepertory::getWmsBasicWarehouseAreaLocationId)
                                        .filter(StringUtils::isNotBlank).collect(Collectors.toList());
                                this.getLocation(strategy, warehouse, param, locationIds, allLocations, outs, materialList, allRepertory, strategyList);
                                if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
                                    strategyList.add(strategy);
                                    continue;
                                }
                            }
                            this.getLocation(strategy, warehouse, param, null, allLocations, outs, materialList, allRepertory, strategyList);
                            if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
                                strategyList.add(strategy);
                                continue;
                            }
                        }
                    }
                }
            }
            // 同库区未找到满足条件的库位 or 入库任务.计划生产站点=空
            if (StringUtils.isBlank(param.getProductSite()) || StringUtils.isBlank(strategy.getWarehouseAreaLocationId())) {
                strategy.setWarehouseAreaId(null);
                BigDecimal width = param.getWidth();
                areas = areas.stream().filter(a -> {
                    if (StringUtils.isBlank(a.getWidthMax()) && StringUtils.isBlank(a.getWidthMin())) {
                        return false;
                    }
                    if (StringUtils.isNotBlank(a.getWidthMin())) {
                        if (width.compareTo(new BigDecimal(a.getWidthMin())) != 1) {
                            return false;
                        }
                    }
                    if (StringUtils.isNotBlank(a.getWidthMax())) {
                        if (width.compareTo(new BigDecimal(a.getWidthMax())) == 1) {
                            return false;
                        }
                    }
                    return true;
                }).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(areas)) {
                    // b.找“位置号”比自己大一位的条料的库位
                    if (StringUtils.isBlank(param.getLocation())) {
                        strategyList.add(strategy);
                        continue;
                    }
                    List<InReceiptStrategyParam> paramBig = params.stream().filter(a ->
                                    StringUtils.isNotBlank(a.getLocation())
                                    && ObjectUtil.equal(Integer.parseInt(param.getLocation()) + 1, a.getLocation()))
                            .collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(paramBig)) {
                        List<InReceiptStrategyWarehouse> collect = strategyList.stream().filter(a ->
                                        ObjectUtil.equal(a.getId(), paramBig.get(0).getId())
                                && StringUtils.isNotBlank(a.getWarehouseAreaLocationId()))
                                .collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect)) {
                            strategy.setWarehouseAreaId(collect.get(0).getWarehouseAreaId());
                            strategy.setWarehouseAreaLocationId(collect.get(0).getWarehouseAreaLocationId());
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                    // c.找“位置号”比自己小一位的条料的库位
                    List<InReceiptStrategyParam> paramSmall = params.stream().filter(a ->
                                    StringUtils.isNotBlank(a.getLocation())
                                    && ObjectUtil.equal(Integer.parseInt(param.getLocation()) - 1, a.getLocation()))
                            .collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(paramSmall)) {
                        List<InReceiptStrategyWarehouse> collect = strategyList.stream().filter(a ->
                                        ObjectUtil.equal(a.getId(), paramSmall.get(0).getId())
                                                && StringUtils.isNotBlank(a.getWarehouseAreaLocationId()))
                                .collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(collect)) {
                            strategy.setWarehouseAreaId(collect.get(0).getWarehouseAreaId());
                            strategy.setWarehouseAreaLocationId(collect.get(0).getWarehouseAreaLocationId());
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                }
                // d.如果找到多个库区，则考虑即时库存中空位量>=【仓库信息】.【库区】设置的值，才是可用库区
    //            List<WmsBasicWarehouseArea> areaList = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
    //                    .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
                if (CollectionUtils.isNotEmpty(areas)) {
                    if (areas.size() == 1) {
                        strategy.setWarehouseAreaId(areas.get(0).getId());
                        // 找库位
                        this.getLocation(strategy, warehouse, param, null, allLocations, outs, materialList, allRepertory, strategyList);
                        strategyList.add(strategy);
                        continue;
                    }
                    areas = areas.stream().filter(a -> {
                        String emptyCapacity = a.getEmptyCapacity();
                        if (StringUtils.isNotBlank(emptyCapacity)) {
                            List<WmsRepertory> repertories = allRepertory.stream().filter(x ->
                                            ObjectUtil.equal(x.getWmsBasicWarehouseAreaId(), a.getId())
                                                    && x.getStockQuantity().compareTo(new BigDecimal("0")) == 1)
                                    .collect(Collectors.toList());
                            Set<String> collect = repertories.stream().map(WmsRepertory::getWmsBasicWarehouseAreaLocationId).collect(Collectors.toSet());
                            long count = allLocations.stream().filter(x ->
                                    ObjectUtil.equal(x.getWmsBasicWarehouseAreaId(), a.getId())
                                            && ObjectUtil.equal("1", x.getStatus())).count();
                            return new BigDecimal(count).subtract(new BigDecimal(collect.size())).compareTo(new BigDecimal(emptyCapacity)) != -1;
                        }
                        return true;
                    }).collect(Collectors.toList());
                    this.getOrderByArea(areas, warehouse);
                    strategy.setWarehouseAreaId(areas.get(0).getId());
                    // 找库位
                    this.getLocation(strategy, warehouse, param, null, allLocations, outs, materialList, allRepertory, strategyList);
                    strategyList.add(strategy);
                    continue;
                }
            }
            strategyList.add(strategy);
        }
        return strategyList;
    }

    /**
     * 横剪退料(条料)
     * @param params
     * @return
     */
    public List<InReceiptStrategyWarehouse> crossShearInReceipt(List<InReceiptStrategyParam> params) {
        List<InReceiptStrategyWarehouse> strategyList = new ArrayList<>();
        List<InReceiptStrategyParam> paramList = params.stream().filter(a ->
                StringUtils.isNotBlank(a.getWarehouseId())).collect(Collectors.toList());
        String warehouseId = "";
        if (CollectionUtils.isNotEmpty(paramList)) {
            warehouseId = paramList.get(0).getWarehouseId();
        }
        List<WmsBasicWarehouseArea> allAreas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(StringUtils.isNotBlank(warehouseId), WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
        List<String> areaIds = allAreas.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
        List<WmsBasicWarehouseAreaLocation> allLocations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, areaIds));
        List<WmsRepertory> allRepertory = repertoryMapper.selectList(new LambdaQueryWrapper<WmsRepertory>()
                .eq(StringUtils.isNotBlank(warehouseId), WmsRepertory::getWmsBasicWarehouseId, warehouseId));
        List<WmsOutReceiptTask> outTasks = outReceiptTaskMapper.selectList(new LambdaQueryWrapper<WmsOutReceiptTask>()
                .in(WmsOutReceiptTask::getDocumentStatus, Arrays.asList("2", "5")));
        List<WmsOutReceiptTaskMaterial> outs = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(outTasks)) {
            List<String> outIds = outTasks.stream().map(WmsOutReceiptTask::getId).collect(Collectors.toList());
            outs = outReceiptTaskMaterialMapper.selectList(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                    .in(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, outIds)
                    .gt(WmsOutReceiptTaskMaterial::getUnoutboundQuantity, 0));
        }
        List<WmsInReceiptTask> inTasks = inReceiptTaskMapper.selectList(new LambdaQueryWrapper<WmsInReceiptTask>()
                .in(WmsInReceiptTask::getDocumentStatus, Arrays.asList("2", "5")));
        List<WmsInReceiptTaskMaterial> materialList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(inTasks)) {
            List<String> ids = inTasks.stream().map(WmsInReceiptTask::getId).collect(Collectors.toList());
            materialList = inReceiptTaskMaterialMapper.selectList(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                    .in(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, ids)
                    .gt(WmsInReceiptTaskMaterial::getUnwarehousedQuantity, 0));   // 未入库数量大于0
        }
        for (InReceiptStrategyParam param : params) {
            InReceiptStrategyWarehouse strategy = new InReceiptStrategyWarehouse();
            strategy.setId(param.getId());

            WmsBasicWarehouse warehouse = this.getWarehouse(param);
            if (warehouse == null || StringUtils.isBlank(warehouse.getId())) {
                strategyList.add(strategy);
                continue;
            }
            strategy.setWarehouseId(warehouse.getId());
            WmsBasicWarehouseArea area = this.getAreaBySite(param.getSite(), warehouse);
            if (area == null) {
                strategyList.add(strategy);
                continue;
            }
            strategy.setWarehouseAreaId(area.getId());
            // 找库位
            if (StringUtils.isNotBlank(param.getProductCode())) {
                List<WmsRepertory> repertoryList = repertoryMapper.selectList(new LambdaQueryWrapper<WmsRepertory>()
                        .eq(WmsRepertory::getWmsBasicWarehouseAreaId, strategy.getWarehouseAreaId())
                        .eq(WmsRepertory::getProductCode, param.getProductCode()));
                if (CollectionUtils.isNotEmpty(repertoryList)) {
                    List<String> locationIds = repertoryList.stream().map(WmsRepertory::getWmsBasicWarehouseAreaLocationId).filter(StringUtils::isNotBlank).collect(Collectors.toList());
                    this.getLocation(strategy, warehouse, param, locationIds, allLocations, outs, materialList, allRepertory, strategyList);
                }
            }
            if (StringUtils.isBlank(strategy.getWarehouseAreaLocationId())) {
                List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                        .eq(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, strategy.getWarehouseAreaId()));
                if (CollectionUtils.isNotEmpty(locations)) {
                    List<String> locationIds = locations.stream().map(WmsBasicWarehouseAreaLocation::getId).collect(Collectors.toList());
                    this.getLocation(strategy, warehouse, param, locationIds, allLocations, outs, materialList, allRepertory, strategyList);
                }
            }
            if (StringUtils.isBlank(strategy.getWarehouseAreaLocationId())) {
                // 没找到库位找下一个目标库区
                WmsBasicWarehouseArea nextArea = this.getNextArea(area, strategy.getWarehouseId());
                if (nextArea == null) {
                    strategyList.add(strategy);
                    continue;
                }
                // 找库位
                strategy.setWarehouseAreaId(nextArea.getId());
                this.getLocation(strategy, warehouse, param, null, allLocations, outs, materialList, allRepertory, strategyList);
                if (StringUtils.isBlank(strategy.getWarehouseAreaLocationId())) {
                    // 没找到库位找下一个目标库区
                    WmsBasicWarehouseArea thirdArea = this.getThirdArea(area, strategy.getWarehouseId());
                    if (thirdArea == null) {
                        strategyList.add(strategy);
                        continue;
                    }
                    // 找库位
                    strategy.setWarehouseAreaId(thirdArea.getId());
                    this.getLocation(strategy, warehouse, param, null, allLocations, outs, materialList, allRepertory, strategyList);
                }
            }
            strategyList.add(strategy);
        }
        return strategyList;
    }

    /**
     * 单据类型=UB调拨入库（根据接收库位判断是否需要刻痕）、刻痕入库（原料）（入已刻痕区）、
     * 刻痕退料（原料）——（入未刻痕区）、纵剪退料（原料）（根据纵剪叫料时候的库区）
     * @param params
     * @return
     */
    public List<InReceiptStrategyWarehouse> allocateInReceipt(List<InReceiptStrategyParam> params) {
        List<InReceiptStrategyWarehouse> strategyList = new ArrayList<>();
        for (InReceiptStrategyParam param : params) {
            InReceiptStrategyWarehouse strategy = new InReceiptStrategyWarehouse();
            strategy.setId(param.getId());
            WmsBasicWarehouse warehouse = this.getWarehouse(param);
            if (warehouse == null || StringUtils.isBlank(warehouse.getId())) {
                strategyList.add(strategy);
                continue;
            }
            strategy.setWarehouseId(warehouse.getId());
            // 此任务已经使用的库位
            List<WmsInReceiptTaskMaterial> usedMaterials = inReceiptTaskMaterialMapper.selectByMainId(param.getTaskId());
            List<String> usedLocationIds = usedMaterials.stream().map(WmsInReceiptTaskMaterial::getWmsBasicWarehouseAreaLocationId).collect(Collectors.toList());
    //        WmsBasicWarehouseArea area = this.getAreaBySite(param.getProductSite(), warehouse);
    //        if (area == null) {
    //            return strategy;
    //        }
    //        strategy.setWarehouseAreaId(area.getId());

            // 纵剪退料（原料）（根据纵剪叫料时候的库区）
            if ("R07".equals(param.getDocumentType())) {
                param.setWarehouseId(strategy.getWarehouseId());
                List<WmsRepertory> repertoryList = repertoryMapper.listByBatchCode(param);
                if (CollectionUtils.isEmpty(repertoryList)) {
                    strategyList.add(strategy);
                    continue;
                }
                strategy.setWarehouseAreaId(repertoryList.get(0).getWmsBasicWarehouseAreaId());
                List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                        .eq(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, repertoryList.get(0).getWmsBasicWarehouseAreaId()));
                // 即时库存中库存量=0
                locations = locations.stream().filter(a -> {
                    Long count = repertoryMapper.selectCount(new LambdaQueryWrapper<WmsRepertory>()
                            .eq(WmsRepertory::getWmsBasicWarehouseAreaLocationId, a.getId()));
                    return count == 0;
                }).collect(Collectors.toList());
                // 小库位
                if (param.getPlanQuantity().compareTo(new BigDecimal("8000")) == -1) {
                    List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) == -1 ).collect(Collectors.toList());
                    if (collect.size() > 0) {
                        if (collect.size() > 1) {
                            // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                            this.getOrderByLocation(collect, warehouse, usedLocationIds);
                        }
                        strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                        strategyList.add(strategy);
                        continue;
                    }
                }
                // 大库位
                List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) != -1).collect(Collectors.toList());
                if (collect.size() > 0) {
                    if (collect.size() > 1) {
                        // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                        this.getOrderByLocation(collect, warehouse, usedLocationIds);
                    }
                    strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                    strategyList.add(strategy);
                    continue;
                }
            }
            if ("SAP".equals(param.getOriginalSystem())) {
                WmsSapWarehouseMap map = sapWarehouseMapService.getOne(new LambdaQueryWrapper<WmsSapWarehouseMap>()
                        .eq(WmsSapWarehouseMap::getSapLocation, param.getSapLocation())
                        .eq(WmsSapWarehouseMap::getFactory, param.getFactory())
                        .last("limit 1"));
                if (map == null) {
                    strategyList.add(strategy);
                    continue;
                }
                List<WmsBasicWarehouseArea> areas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                        .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, strategy.getWarehouseId()));
                if ("2".equals(map.getType())) {
                    // 刻痕库区
                    List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> "2".equals(a.getAreaType())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(list)) {
                        List<String> ids = list.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
                        List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, ids));
                        // 即时库存中库存量=0
                        locations = locations.stream().filter(a -> {
                            Long count = repertoryMapper.selectCount(new LambdaQueryWrapper<WmsRepertory>()
                                    .eq(WmsRepertory::getWmsBasicWarehouseAreaLocationId, a.getId()));
                            return count == 0;
                        }).collect(Collectors.toList());
                        // 小库位
                        if (param.getPlanQuantity().compareTo(new BigDecimal("8000")) == -1) {
                            List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) == -1).collect(Collectors.toList());
                            this.getAreaAndLocation(strategy, collect, param, warehouse, usedLocationIds);
                            if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
                                strategyList.add(strategy);
                                continue;
                            }
                        }
                        // 大库位
                        List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) != -1).collect(Collectors.toList());
                        this.getAreaAndLocation(strategy, collect, param, warehouse, usedLocationIds);
                        if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                }
                // 非刻痕库区
                List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> "1".equals(a.getAreaType())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(list)) {
                    List<String> ids = list.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
                    List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                            .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, ids));
                    // 即时库存中库存量=0
                    locations = locations.stream().filter(a -> {
                        Long count = repertoryMapper.selectCount(new LambdaQueryWrapper<WmsRepertory>()
                                .eq(WmsRepertory::getWmsBasicWarehouseAreaLocationId, a.getId()));
                        return count == 0;
                    }).collect(Collectors.toList());
                    // 小库位
                    if (param.getPlanQuantity().compareTo(new BigDecimal("8000")) == -1) {
                        List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) == -1).collect(Collectors.toList());
                        if (collect.size() > 0) {
                            if (collect.size() > 1) {
                                // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                                this.getOrderByLocation(collect, warehouse, usedLocationIds);
                            }
                            strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                            strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                    // 大库位
                    List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) != -1).collect(Collectors.toList());
                    if (collect.size() > 0) {
                        if (collect.size() > 1) {
                            // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                            this.getOrderByLocation(collect, warehouse, usedLocationIds);
                        }
                        strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                        strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                        strategyList.add(strategy);
                        continue;
                    }
                }
            }
            if ("MES".equals(param.getOriginalSystem())) {
                List<WmsBasicWarehouseArea> areas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                        .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, strategy.getWarehouseId()));
                String areaType = "";
                if ("R07".equals(param.getDocumentType()) && StringUtils.isNotBlank(strategy.getWarehouseAreaId())) {
                    // 纵剪退料（原料），未找到纵剪叫料时候的库区
                    WmsBasicWarehouseArea area = warehouseAreaMapper.selectById(strategy.getWarehouseAreaId());
                    areaType = area.getAreaType();
                    strategy.setWarehouseAreaId(null);
                }
                if ("R03".equals(param.getDocumentType()) || ("R07".equals(param.getDocumentType()) && "2".equals(areaType))) {
                    // 刻痕库区
                    List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> "2".equals(a.getAreaType())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(list)) {
                        List<String> ids = list.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
                        List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, ids));
                        // 即时库存中库存量=0
                        locations = locations.stream().filter(a -> {
                            Long count = repertoryMapper.selectCount(new LambdaQueryWrapper<WmsRepertory>()
                                    .eq(WmsRepertory::getWmsBasicWarehouseAreaLocationId, a.getId()));
                            return count == 0;
                        }).collect(Collectors.toList());
                        // 小库位
                        if (param.getPlanQuantity().compareTo(new BigDecimal("8000")) == -1) {
                            List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) == -1).collect(Collectors.toList());
                            this.getAreaAndLocation(strategy, collect, param, warehouse, usedLocationIds);
                            if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
                                strategyList.add(strategy);
                                continue;
                            }
                        }
                        // 大库位
                        List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) != -1).collect(Collectors.toList());
                        this.getAreaAndLocation(strategy, collect, param, warehouse, usedLocationIds);
                        if (StringUtils.isNotBlank(strategy.getWarehouseAreaLocationId())) {
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                    // 非刻痕库区
                    list = areas.stream().filter(a -> "1".equals(a.getAreaType())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(list)) {
                        List<String> ids = list.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
                        List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, ids));
                        // 即时库存中库存量=0
                        locations = locations.stream().filter(a -> {
                            Long count = repertoryMapper.selectCount(new LambdaQueryWrapper<WmsRepertory>()
                                    .eq(WmsRepertory::getWmsBasicWarehouseAreaLocationId, a.getId()));
                            return count == 0;
                        }).collect(Collectors.toList());
                        // 小库位
                        if (param.getPlanQuantity().compareTo(new BigDecimal("8000")) == -1) {
                            List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) == -1).collect(Collectors.toList());
                            if (collect.size() > 0) {
                                if (collect.size() > 1) {
                                    // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                                    this.getOrderByLocation(collect, warehouse, usedLocationIds);
                                }
                                strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                                strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                                strategyList.add(strategy);
                                continue;
                            }
                        }
                        // 大库位
                        List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) != -1).collect(Collectors.toList());
                        if (collect.size() > 0) {
                            if (collect.size() > 1) {
                                // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                                this.getOrderByLocation(collect, warehouse, usedLocationIds);
                            }
                            strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                            strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                }
                if ("R05".equals(param.getDocumentType()) || ("R07".equals(param.getDocumentType()) && "1".equals(areaType))) {
                    // 非刻痕库区
                    List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> "1".equals(a.getAreaType())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(list)) {
                        List<String> ids = list.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
                        List<WmsBasicWarehouseAreaLocation> locations = warehouseAreaLocationMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, ids));
                        // 即时库存中库存量=0
                        locations = locations.stream().filter(a -> {
                            Long count = repertoryMapper.selectCount(new LambdaQueryWrapper<WmsRepertory>()
                                    .eq(WmsRepertory::getWmsBasicWarehouseAreaLocationId, a.getId()));
                            return count == 0;
                        }).collect(Collectors.toList());
                        // 小库位
                        if (param.getPlanQuantity().compareTo(new BigDecimal("8000")) == -1) {
                            List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) == -1).collect(Collectors.toList());
                            if (collect.size() > 0) {
                                if (collect.size() > 1) {
                                    // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                                    this.getOrderByLocation(collect, warehouse, usedLocationIds);
                                }
                                strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                                strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                                strategyList.add(strategy);
                                continue;
                            }
                        }
                        // 大库位
                        List<WmsBasicWarehouseAreaLocation> collect = locations.stream().filter(a -> a.getMaxCapacity() != null && a.getMaxCapacity().compareTo(new BigDecimal("8000")) != -1).collect(Collectors.toList());
                        if (collect.size() > 0) {
                            if (collect.size() > 1) {
                                // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                                this.getOrderByLocation(collect, warehouse, usedLocationIds);
                            }
                            strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                            strategy.setWarehouseAreaLocationId(collect.get(0).getId());
                            strategyList.add(strategy);
                            continue;
                        }
                    }
                }
            }
            strategyList.add(strategy);
        }
        return strategyList;
    }

    /**
     * 找仓库
     * @param param
     * @return
     */
    private WmsBasicWarehouse getWarehouse(InReceiptStrategyParam param) {
        if (StringUtils.isNotBlank(param.getWarehouseId())) {
            WmsBasicWarehouse warehouse = warehouseMapper.selectById(param.getWarehouseId());
            return warehouse;
        }
        String warehouseId = "";
        if ("SAP".equals(param.getOriginalSystem())) {
            // 来源系统=SAP
            if (StringUtils.isNotBlank(param.getSapLocation())) {
                WmsSapWarehouseMap map = sapWarehouseMapService.getOne(new LambdaQueryWrapper<WmsSapWarehouseMap>()
                        .eq(WmsSapWarehouseMap::getSapLocation, param.getSapLocation())
                        .eq(WmsSapWarehouseMap::getFactory, param.getFactory())
                        .last("limit 1"));
                if (map != null) {
                    warehouseId = map.getWmsBasicWarehouseId();
                }
            }
        }
        if ("MES".equals(param.getOriginalSystem())) {
            if (StringUtils.isNotBlank(param.getWarehouseId())) {
                // 如果mes传了仓库则直接取传的仓库
                warehouseId = param.getWarehouseId();
            }
//            if (("R25".equals(param.getDocumentType()) || "R26".equals(param.getDocumentType()))) {
//                // 来源系统=MES并且 单据类型= 成品入库-铁心、成品入库-定尺
//                MesSite one = mesSiteService.getOne(new LambdaQueryWrapper<MesSite>()
//                        .eq(MesSite::getAskMaterialSite, param.getProductSite())
//                        .last("limit 1"));
//                if (one != null) {
//                    warehouseId = one.getMesInWarehouseId();
//                }
//            } else {
//                warehouseId = param.getWarehouseId();
//            }
        }
        if (StringUtils.isBlank(warehouseId)) {
            // 物料信息.默认仓库
            if (StringUtils.isBlank(param.getMaterialId())) {
                return null;
            }
            List<WmsBasicMaterialStockInfo> list = materialStockInfoService.list(new LambdaQueryWrapper<WmsBasicMaterialStockInfo>()
                    .eq(WmsBasicMaterialStockInfo::getWmsBasicMaterialId, param.getMaterialId()));
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            warehouseId = list.get(0).getWarehouseId();
        }
        if (StringUtils.isBlank(warehouseId)) {
            return null;
        }
        WmsBasicWarehouse warehouse = warehouseMapper.selectById(warehouseId);
        return warehouse;
    }

    /**
     * 基准点往四周扩散算法
     *
     * @param areaId
     * @return
     */
    private List<WmsBasicWarehouseAreaLocation> getDatumList(String areaId,
                                                             List<WmsBasicWarehouseAreaLocation> locations,
                                                             List<WmsBasicWarehouseAreaLocation> allLocations) {
        List<WmsBasicWarehouseAreaLocation> datumLocations = allLocations.stream().filter(a ->
                ObjectUtil.equal(areaId, a.getWmsBasicWarehouseAreaId())
                && ObjectUtil.equal("1", a.getIsDatum()))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(datumLocations)) {
            return Collections.emptyList();
        }
        WmsBasicWarehouseAreaLocation datum = datumLocations.get(0);
        String[] datumArray = datum.getCode().split("-");
        if (datumArray.length < 4) {
            return Collections.emptyList();
        }
        BigDecimal datum1 = new BigDecimal(this.getNumString(datumArray[datumArray.length - 3]));   // 行
        BigDecimal datum2 = new BigDecimal(datumArray[datumArray.length - 1]);  // 列
        for (WmsBasicWarehouseAreaLocation location : locations) {
            String[] split = location.getCode().split("-");
            BigDecimal decimal1 = new BigDecimal(this.getNumString(split[split.length - 3]));   // 行
            BigDecimal decimal2 = new BigDecimal(split[split.length - 1]);  // 列
            BigDecimal decimal3 = new BigDecimal(split[split.length - 2]);  // 层
            BigDecimal subtract1 = decimal1.subtract(datum1);
            BigDecimal subtract2 = decimal2.subtract(datum2);
            BigDecimal result = subtract1.multiply(subtract1).add(subtract2.multiply(subtract2)).multiply(BigDecimal.valueOf(10)).add(decimal3);
            location.setResult(result);
        }
        List<WmsBasicWarehouseAreaLocation> list = locations.stream().sorted(Comparator.comparing(WmsBasicWarehouseAreaLocation::getResult)).collect(Collectors.toList());
        return list;
    }

    /**
     * 将字符串转为数字字符串
     * @param oldString
     * @return
     */
    private String getNumString(String oldString) {
        String newString = oldString.replace("A", "")
                .replace("B", "")
                .replace("C", "")
                .replace("D", "")
                .replace("a", "")
                .replace("b", "")
                .replace("c", "")
                .replace("d", "");
        return newString;
    }

    /**
     * 校验容量和宽度
     * @param locationIds
     * @param warehouse
     * @param param
     * @return
     */
    private List<String> checkWeightWidth(List<String> locationIds,
                                          WmsBasicWarehouse warehouse,
                                          InReceiptStrategyParam param,
                                          List<WmsInReceiptTaskMaterial> materialList,
                                          List<WmsBasicWarehouseAreaLocation> locationList,
                                          List<WmsRepertory> wmsRepertoryList,
                                          List<InReceiptStrategyWarehouse> strategyList) {
        List<String> locationIdList = new ArrayList<>();
        for (String locationId : locationIds) {
            WmsBasicWarehouseAreaLocation location = locationList.stream().filter(a -> ObjectUtil.equal(locationId, a.getId())).collect(Collectors.toList()).get(0);
            List<WmsRepertory> repertories = wmsRepertoryList.stream().filter(a -> ObjectUtil.equal(locationId, a.getWmsBasicWarehouseAreaLocationId())).collect(Collectors.toList());

            // 预计入库库存
            List<InReceiptStrategyWarehouse> used = strategyList.stream().filter(a -> ObjectUtil.equal(a.getWarehouseAreaLocationId(), locationId)).collect(Collectors.toList());
            List<String> usedIds = used.stream().map(InReceiptStrategyWarehouse::getId).collect(Collectors.toList());
            List<WmsInReceiptTaskMaterial> materials = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(materialList)) {
                materials = materialList.stream().filter(a -> ObjectUtil.equal(locationId, a.getWmsBasicWarehouseAreaLocationId())
                || usedIds.contains(a.getId())).collect(Collectors.toList());
            }

            // 判断宽度
            if ("1".equals(warehouse.getOtherCapacityControl())) {
                BigDecimal maxWidth = location.getMaxWidth();
                if (maxWidth != null) {
                    BigDecimal width = repertories.stream().filter(a -> a.getWidth() != null).map(WmsRepertory::getWidth).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal wid = new BigDecimal("0");   //预计入库条料宽度
                    if (CollectionUtils.isNotEmpty(materials)) {
                        // 预计入库库存宽度
                        wid = materials.stream().filter(a -> a.getWidth() != null).map(WmsInReceiptTaskMaterial::getWidth).reduce(BigDecimal.ZERO, BigDecimal::add);
                    }
                    BigDecimal bigDecimal = new BigDecimal(repertories.size() + materials.size() + 2);
                    BigDecimal toolLength = warehouse.getToolLength();  //条料间距
                    if (toolLength != null) {
                        BigDecimal s = maxWidth.subtract(width).subtract(wid).subtract(bigDecimal.multiply(toolLength));
                        if (s.compareTo(param.getWidth()) <= -1) {
                            continue;
                        }
                    }
                }
            }
            // 判断库存容量
            if (StringUtils.isNotBlank(warehouse.getCapacityControlUnit())) {
                BigDecimal wid = new BigDecimal("0");   //预计入库条料容量
                // 库存单位
                if (Arrays.asList(warehouse.getCapacityControlUnit().split(",")).contains("1")) {
                    if (CollectionUtils.isNotEmpty(materials)) {
                        wid = materials.stream().map(WmsInReceiptTaskMaterial::getUnwarehousedQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
                    }
                    BigDecimal stockQuantity = repertories.stream().map(WmsRepertory::getStockQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal maxCapacity = location.getMaxCapacity();
                    if (maxCapacity != null) {
                        if (maxCapacity.subtract(stockQuantity).subtract(wid).compareTo(param.getUnwarehousedQuantity()) <= -1) {
                            continue;
                        }
                    }
                }
                // 辅助单位
                if (Arrays.asList(warehouse.getCapacityControlUnit().split(",")).contains("2")) {
                    if (CollectionUtils.isNotEmpty(materials)) {
                        wid = materials.stream().map(WmsInReceiptTaskMaterial::getUnwarehousedQuantityAssist).reduce(BigDecimal.ZERO, BigDecimal::add);
                    }
                    BigDecimal stockQuantity = repertories.stream().map(WmsRepertory::getStockQuantitySub).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal maxCapacity = location.getMaxCapacityAssist();
                    if (maxCapacity != null) {
                        if (maxCapacity.subtract(stockQuantity).subtract(wid).compareTo(param.getUnwarehousedQuantityAssist()) <= -1) {
                            continue;
                        }
                    }
                }
            }
            locationIdList.add(locationId);
        }
        return locationIdList;
    }

    /**
     * 找库位
     * 无出库任务的库位 and 库位.临时移库用=False   and 状态=启用 and  现有库存库存容量+预计入库库存容量
     * and 现有库存宽度+预计入库库存宽度
     * @param strategy
     * @param warehouse
     * @param param
     * @return
     */
    private void getLocation(InReceiptStrategyWarehouse strategy,
                             WmsBasicWarehouse warehouse, InReceiptStrategyParam param,
                             List<String> locationIds,
                             List<WmsBasicWarehouseAreaLocation> allLocations,
                             List<WmsOutReceiptTaskMaterial> outs,
                             List<WmsInReceiptTaskMaterial> materialList,
                             List<WmsRepertory> allRepertory,
                             List<InReceiptStrategyWarehouse> strategyList) {
        // 无同计划生产工单号或者宽度、重量不满足，找其他满足如下条件的库位：
        // 无出库任务的库位 and 库位.临时移库用=False   and 状态=启用
        // and  现有库存库存容量+预计入库库存容量 and 现有库存宽度+预计入库库存宽度
        // 不是临时移库并且启用的库位
        List<WmsBasicWarehouseAreaLocation> locations;
        if (CollectionUtils.isEmpty(locationIds)) {
            locations = allLocations.stream().filter(a ->
                            ObjectUtil.equal(strategy.getWarehouseAreaId(), a.getWmsBasicWarehouseAreaId())
                            && ObjectUtil.equal("0", a.getIsTemporaryMove())
                            && ObjectUtil.equal("1", a.getStatus()))
                    .collect(Collectors.toList());
        } else {
            locations = allLocations.stream().filter(a -> locationIds.contains(a.getId())
                            && ObjectUtil.equal(strategy.getWarehouseAreaId(), a.getWmsBasicWarehouseAreaId())
                            && ObjectUtil.equal("0", a.getIsTemporaryMove())
                            && ObjectUtil.equal("1", a.getStatus()))
                    .collect(Collectors.toList());
        }
        // 有出库任务的库位
        List<WmsBasicWarehouseAreaLocation> list = locations;
        if (CollectionUtils.isNotEmpty(outs)) {
            Set<String> set = outs.stream().map(WmsOutReceiptTaskMaterial::getWmsBasicWarehouseAreaLocationId).collect(Collectors.toSet());
            list = locations.stream().filter(a -> !set.contains(a.getId())).collect(Collectors.toList());
        }
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> collect = list.stream().map(WmsBasicWarehouseAreaLocation::getId).collect(Collectors.toList());
            // 库存容量和剩余宽度满足
            List<String> locationIdList = this.checkWeightWidth(collect, warehouse, param, materialList, allLocations, allRepertory, strategyList);
            List<WmsBasicWarehouseAreaLocation> datumList = this.getDatumList(strategy.getWarehouseAreaId(), warehouseAreaLocationMapper.selectBatchIds(locationIdList), allLocations);
            if (CollectionUtils.isNotEmpty(datumList)) {
                strategy.setWarehouseAreaLocationId(datumList.get(0).getId());
            }
        }
    }

    /**
     * 根据站点找库区
     * @param site
     * @param warehouse
     * @return
     */
    private WmsBasicWarehouseArea getAreaBySite(String site, WmsBasicWarehouse warehouse) {
        if (StringUtils.isBlank(site)) {
            return null;
        }
        List<DictModel> sites = iSysBaseAPI.getDictItems("cross_cut_material_site");    // 生产站点
        List<DictModel> siteList = sites.stream().filter(a -> site.equals(a.getText())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(siteList)) {
            return null;
        }
        List<WmsBasicWarehouseArea> areas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouse.getId()));
        List<WmsBasicWarehouseArea> collect = areas.stream().filter(a -> StringUtils.isNotBlank(a.getProductSite()) && Arrays.asList(a.getProductSite().split(",")).contains(siteList.get(0).getValue())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return null;
        }
        return collect.get(0);
    }

    /**
     * 库区按入库策略排序
     * @param areas
     * @param warehouse
     */
    private void getOrderByArea(List<WmsBasicWarehouseArea> areas, WmsBasicWarehouse warehouse) {
        if ("2".equals(warehouse.getWarehouseStrategy())) {
            // 从大到小
            areas = areas.stream().sorted(Comparator.comparing(WmsBasicWarehouseArea::getOrderIndex).reversed()).collect(Collectors.toList());
        } else if ("3".equals(warehouse.getWarehouseStrategy())) {
            // 绝对值从小到大
            for (WmsBasicWarehouseArea location : areas) {
                location.setOrderIndex(Math.abs(location.getOrderIndex()));
            }
            areas = areas.stream().sorted(Comparator.comparing(WmsBasicWarehouseArea::getOrderIndex)).collect(Collectors.toList());
        } else if ("4".equals(warehouse.getWarehouseStrategy())) {
            // 绝对值从大到小
            for (WmsBasicWarehouseArea location : areas) {
                location.setOrderIndex(Math.abs(location.getOrderIndex()));
            }
            areas = areas.stream().sorted(Comparator.comparing(WmsBasicWarehouseArea::getOrderIndex).reversed()).collect(Collectors.toList());
        } else {
            // 从小到大
            areas = areas.stream().sorted(Comparator.comparing(WmsBasicWarehouseArea::getOrderIndex)).collect(Collectors.toList());
        }
    }

    /**
     * 库位按入库策略排序
     * @param locations
     * @param warehouse
     */
    private void getOrderByLocation(List<WmsBasicWarehouseAreaLocation> locations, WmsBasicWarehouse warehouse, List<String> usedLocationIds) {
        // 排除已经选择的库位
        locations = locations.stream().filter(a -> !usedLocationIds.contains(a.getId())).collect(Collectors.toList());
        if ("2".equals(warehouse.getWarehouseStrategy())) {
            // 从大到小
            locations = locations.stream().sorted(Comparator.comparing(WmsBasicWarehouseAreaLocation::getOrderIndex).reversed()).collect(Collectors.toList());
        } else if ("3".equals(warehouse.getWarehouseStrategy())) {
            // 绝对值从小到大
            for (WmsBasicWarehouseAreaLocation location : locations) {
                location.setOrderIndex(Math.abs(location.getOrderIndex()));
            }
            locations = locations.stream().sorted(Comparator.comparing(WmsBasicWarehouseAreaLocation::getOrderIndex)).collect(Collectors.toList());
        } else if ("4".equals(warehouse.getWarehouseStrategy())) {
            // 绝对值从大到小
            for (WmsBasicWarehouseAreaLocation location : locations) {
                location.setOrderIndex(Math.abs(location.getOrderIndex()));
            }
            locations = locations.stream().sorted(Comparator.comparing(WmsBasicWarehouseAreaLocation::getOrderIndex).reversed()).collect(Collectors.toList());
        } else {
            // 从小到大
            locations = locations.stream().sorted(Comparator.comparing(WmsBasicWarehouseAreaLocation::getOrderIndex)).collect(Collectors.toList());
        }
    }

    /**
     * 获取第二个目标库区
     * 目标库区A:ABC
     * 目标库区B:BCA
     * 目标库区C:CBA
     * @param warehouseId
     * @return
     */
    private WmsBasicWarehouseArea getNextArea(WmsBasicWarehouseArea area, String warehouseId) {
        List<WmsBasicWarehouseArea> areas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
        String[] split = area.getCode().split(",");
        if (split[split.length - 1].contains("A")) {
            List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> {
                String[] array = a.getCode().split("-");
                return array[array.length - 1].contains("B");
            }).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
        }
        if (split[split.length - 1].contains("B")) {
            List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> {
                String[] array = a.getCode().split("-");
                return array[array.length - 1].contains("C");
            }).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
        }
        if (split[split.length - 1].contains("C")) {
            List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> {
                String[] array = a.getCode().split("-");
                return array[array.length - 1].contains("B");
            }).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
        }
        return null;
    }

    /**
     * 获取第三个目标库区
     * 目标库区A:ABC
     * 目标库区B:BCA
     * 目标库区C:CBA
     * @param warehouseId
     * @return
     */
    private WmsBasicWarehouseArea getThirdArea(WmsBasicWarehouseArea area, String warehouseId) {
        List<WmsBasicWarehouseArea> areas = warehouseAreaMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
        String[] split = area.getCode().split(",");
        if (split[split.length - 1].contains("A")) {
            List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> {
                String[] array = a.getCode().split("-");
                return array[array.length - 1].contains("C");
            }).collect(Collectors.toList());
            return list.get(0);
        }
        if (split[split.length - 1].contains("B")) {
            List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> {
                String[] array = a.getCode().split("-");
                return array[array.length - 1].contains("A");
            }).collect(Collectors.toList());
            return list.get(0);
        }
        if (split[split.length - 1].contains("C")) {
            List<WmsBasicWarehouseArea> list = areas.stream().filter(a -> {
                String[] array = a.getCode().split("-");
                return array[array.length - 1].contains("A");
            }).collect(Collectors.toList());
            return list.get(0);
        }
        return null;
    }

    /**
     * 如果 找到多个库位对应不同库区   and 入库任务.计划生产站点<>空，则找计划生产站点对应的库区，根据计划生产站点到【仓库信息】.【库区】找到对应库区编号，
     * or 如果找到多个库位对应不同库区   and 入库任务.计划生产站点=空，按【仓库信息】.【库区】的正序排序(根据仓库.入库策略)。
     * 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
     * @param collect
     * @param param
     * @param warehouse
     * @return
     */
    private void getAreaAndLocation(InReceiptStrategyWarehouse strategy, List<WmsBasicWarehouseAreaLocation> collect, InReceiptStrategyParam param, WmsBasicWarehouse warehouse, List<String> usedLocationIds) {
        if (collect.size() > 0) {
            if (collect.size() > 1) {
                String areaId = collect.get(0).getWmsBasicWarehouseAreaId();
                if (collect.stream().anyMatch(a -> !a.getWmsBasicWarehouseAreaId().equals(areaId))) {
                    // 多个库位对应不同库区
                    if (StringUtils.isNotBlank(param.getProductSite())) {
                        // 生产站点不为空
                        WmsBasicWarehouseArea warehouseArea = this.getAreaBySite(param.getProductSite(), warehouse);
                        if (warehouseArea != null) {
                            collect = collect.stream().filter(a -> warehouseArea.getId().equals(a.getWmsBasicWarehouseAreaId())).collect(Collectors.toList());
                            if (CollectionUtils.isNotEmpty(collect)) {
                                this.getOrderByLocation(collect, warehouse, usedLocationIds);
                            }
                        }
                    } else {
                        // 生产站点为空
                        Set<String> set = collect.stream().map(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId).collect(Collectors.toSet());
                        List<WmsBasicWarehouseArea> warehouseAreas = warehouseAreaMapper.selectBatchIds(set);
                        if (CollectionUtils.isNotEmpty(warehouseAreas)) {
                            this.getOrderByArea(warehouseAreas, warehouse);
                            collect = collect.stream().filter(a -> warehouseAreas.get(0).getId().equals(a.getWmsBasicWarehouseAreaId())).collect(Collectors.toList());
                            this.getOrderByLocation(collect, warehouse, usedLocationIds);
                        }
                    }
                } else {
                    // 找到多个库位，按【仓库信息】.【库位】的正序排序(根据仓库.入库策略)
                    this.getOrderByLocation(collect, warehouse, usedLocationIds);
                }
            }
            if (CollectionUtils.isNotEmpty(collect)) {
                strategy.setWarehouseAreaId(collect.get(0).getWmsBasicWarehouseAreaId());
                strategy.setWarehouseAreaLocationId(collect.get(0).getId());
            }
        }
    }
    public List<InReceiptStrategyWarehouse> strategyLogicInvoke(List<InReceiptStrategyParam> params, String documentType){
        long start = System.currentTimeMillis();
        log.info("入库策略调用开始,参数:{},单据类型:{}", params, documentType);
        List<String> strategy1 = Arrays.asList("R12", "R101");
        List<String> strategy2 = Arrays.asList("R14");
        List<String> strategy3 = Arrays.asList("R03", "R05", "R07","R02","R43","R29");
        List<String> strategy4 = Arrays.asList("R25", "R26","R01");
        List<InReceiptStrategyWarehouse> strategyList = new ArrayList<>();
        try {
            if (strategy1.contains(documentType)){
                strategyList = stripMaterialInReceipt(params);
            }
            if (strategy2.contains(documentType)){
                strategyList = crossShearInReceipt(params);
            }
            if (strategy3.contains(documentType)){
                strategyList = allocateInReceipt(params);
            }
            if (strategy4.contains(documentType)){
                strategyList = warehouseStrategy(params);
            }
        } catch (Exception e) {
            log.error("入库策略报错，入参为:"+ JSON.toJSONString(params) +"，报错为:", e );
        }
        long end = System.currentTimeMillis();
        log.info("入库策略调用结束,共耗时:{}",end-start+"ms");
        return strategyList;
    }

}
